import {Crypt} from 'c3-util'
import axios from 'axios'
export default class TestUtil {
    static test():string {
        return 'hello world'
    }
    static md5(str:string):string {
        return Crypt.hex_md5(str)
    }
    /**
     * 获取 连接imrServer的token
     * @param companyId 
     * @param extendStaffId 
     * @param companyCode 
     * @returns {"result":"0",
     *           "imConfig":{"domain":"kfas3","pwd":"mXX20wVRM8tUa6tav3UO/zu0/vkL0PGm1stZwfYeD4lsCIV5Tuj1kg==","username":"90159110003377","order_name":"20170504"},
     *          "wsServers":[{"port":"443","ip":"192.168.0.17","context":"imrServer/websocket","type":"wss"}],
     *          "iceServers":[],
     *          "staffId":"893093795463757824",
     *          "resultMsg":"成功",
     *          "token":{"expireTime":"1800","accessToken":"1CDD53170312C696523467B7BF289E81"}}
     */
    static async imrServerToken(companyId:string,extendStaffId:string,companyCode:string) {
        let timestamp='2021110414340000'
        const version="1.0.1"
        const internetFlag="0"
        const token=this.md5(companyId+extendStaffId+timestamp+companyCode).toUpperCase()
        let params:Array<string>=['companyId='+companyId,
                                  'version='+version,
                                  'extendStaffId='+extendStaffId,
                                  'timeStamp='+timestamp,
                                  'token='+token,
                                  'internetFlag='+internetFlag]
        console.log('params:'+params.join("&"))
        let resultObject =await axios.get('http://192.168.0.17/c3iservuc/jsonp/getImrServerSpdnByJson?'+params.join('&'))
                                             .then(response=>{
                                                 console.log('spdn return:'+response.data)
                                                 return new Promise((resolve,reject)=>{
                                                     if(response.data.result=='0')
                                                       resolve(response.data)
                                                    else
                                                       reject(response.data)
                                                 })
                                             }).catch((error=>{
                                                 console.log('request error,'+error)
                                                 return new Promise((resolve,reject)=>{
                                                     resolve({result:'-999',resultMsg:'服务器连接失败'})
                                                 })
                                             }))
        return resultObject
                         
    }
        /**
     * 等待指定的时间
     * @param ms
     */
    static async sleep(ms: number) {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve('');
            }, ms)
        });
    }

}